package com.tz.dao;

import com.tz.entity.Product;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Administrator on 16-7-28.
 */
@Repository
@Transactional
public class ProductDaoImpl implements ProductDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    @Override
    public List<Product> findByDetachedCriteria(DetachedCriteria dc, int maxResults) {
        Criteria criteria = dc.getExecutableCriteria(this.getSession());
        criteria.setMaxResults(maxResults);
        return criteria.list();
    }

    @Override
    public void save(Product product) {
        this.getSession().save(product);
    }
}
